﻿package com.lic.finance;

import com.lic.finance.excel.ExcelUtils;
import com.lic.finance.po.OutgoItem;
import com.lic.finance.utils.Util;
import com.lic.finance.xml.XMLUtils;

import java.util.HashMap;
import java.util.List;

public class main {
	public static final String mPath = "../wacai_2011-05.xls";
	public static final String mXMLPath = "source_files/2009_5_31.xml";
	public static final String MONEYWISE_FILE_PATH = "source_files/20040101_moneywise.xml";
    public static final String TEMPLATE_FILE_PATH = "source_files/wacai365_outgo.xls";
    public static final String CREATION_FILE_PATH = "source_files/creation.xls";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

        ExcelUtils eu = new ExcelUtils();
        eu.setTemplateFile(TEMPLATE_FILE_PATH);
        eu.setCreationFile(CREATION_FILE_PATH);

		XMLUtils xr = new XMLUtils(MONEYWISE_FILE_PATH);
		List<OutgoItem> list = xr.getOutgoItems(2);
		System.out.println("Find " + list.size() + " items.");

		eu.readEnumWacai();
		String[] eProject = eu.getProjects();
		String[] eWriteOff = eu.getWriteOff();

		HashMap<String, String> categoryMap = Util.getMoneywiseToWacaiCategoryMap();
		HashMap<String, String> accountMap = Util.getMoneywiseToWacaiAccount();

		for(int i = 0; i < list.size(); i++) {
		    OutgoItem item = list.get(i);
		    //Reset write off
		    item.setWriteOff(eWriteOff[0]);
		    //Reset project
		    item.setProject(eProject[0]);
		    //Reset category
		    item.setCategory(categoryMap.get(item.getCategory()));
		    // Reset account
		    item.setAccount(accountMap.get(item.getAccount()));
		}

		// The excel max item counts is 500.
		if (list.size() >= 500) {
		    eu.writeToExcel(list);
		} else {
		    int leaveItems = list.size();
		    for (int i=0; i < list.size()/500; i++) {
		        eu.setCreationFile(CREATION_FILE_PATH.split(".")[0] + "_" + i + "xls");
		        if (leaveItems > 500) {
		            eu.writeToExcel(list.subList(i*500, (i+1)*500-1));
		        } else {
		            eu.writeToExcel(list.subList(list.size()-leaveItems-1, list.size()-1));
		        }
		        leaveItems = leaveItems - 500;
		    }
		}

        eu.closeAll();
        System.out.println("Have writen the data into excel!");
	}

}
